package com.hfy.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hfy.model.po.user.MessagePo;
import com.hfy.model.ws.privateMessage.CommonMessage;
import com.hfy.user.mapper.MessageMapper;
import com.hfy.user.service.MessageService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import result.Result;

import java.util.List;

/**
 * @ author 韩
 * time: 2024/11/3 14:46
 */

@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, MessagePo> implements MessageService {

    @Override
    public Result getRecordBySessionId(String sessionId) {
        // 可以检验一下session表存不存在该id
        if (StringUtils.isEmpty(sessionId)) return Result.ok();

        LambdaQueryWrapper<MessagePo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.select(MessagePo::getId, MessagePo::getData, MessagePo::getSendTime, MessagePo::getType, MessagePo::getAccount, MessagePo::getToAccount)
                .eq(MessagePo::getIsDie, 0).eq(MessagePo::getSessionId, sessionId);
        List<MessagePo> list = list(queryWrapper);
        list.forEach((item) -> {
            CommonMessage message = JSON.parseObject(item.getData().toString(), CommonMessage.class);

            item.setData(message);
        });
        return Result.ok(list);
    }

}
